package com.tianyu.inspection.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tianyu.inspection.common.Constants;
import com.tianyu.inspection.common.enums.RoState;
import com.tianyu.inspection.common.annotation.Permission;
import com.tianyu.inspection.entity.Role;
import com.tianyu.inspection.entity.model.OperateMo;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.entity.vo.Ro;
import com.tianyu.inspection.entity.vo.ServiceRo;
import com.tianyu.inspection.service.api.IRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description 用户登录接口
 * @Date 2021/1/7 - 11:48
 **********************************/

@RestController
@RequestMapping("/api/2.0/role/admin")
public class RoleController {
    
    @Resource
    private IRoleService roleService;

    @GetMapping(value = "/list")
    @Permission(value = 1,tags = Constants.API_SUPER)
    public Ro<IPage<Role>> getPageList(HttpServletRequest request){
        QueryMo queryMo = QueryMo.getQueryMo(request);
        Ro<IPage<Role>> ro = new Ro<>();
        queryMo.build(Role.class,null);
        return ro.setContent(roleService.searchListPage(queryMo));
    }

    @GetMapping(value = "/{id}")
    @Permission(value = 1,tags = Constants.API_SUPER)
    public Ro<Role> getOne(@PathVariable Integer id){
        Ro<Role> ro = new Ro<>();
        if (Objects.isNull(id)){
            return ro.setResultEnum(RoState.PARAMS_MISS_ERROR);
        }
        QueryMo queryMo = new QueryMo();
        queryMo.put("id",id);
        queryMo.build(Role.class,null);
        Role role = roleService.selectOne(queryMo);
        if (Objects.isNull(role)){
            ro.setMessage("未查询到数据");
        }else {
            ro.setContent(role);
        }
        return ro;
    }

    @PostMapping(value = "/insert")
    @Permission(value = 1,tags = Constants.API_SUPER)
    public Ro<Role> insert(@RequestBody OperateMo operateMo){
        Ro<Role> ro = new Ro<>();
        operateMo.createBuild(Role.class,null);
        if (operateMo.getEntity().size()==0){
            return ro.setResultEnum(RoState.FAIL).setMessage("无操作参数传入,请传参后重试");
        }
        if (!roleService.insert(operateMo)){
            ro.setResultEnum(RoState.FAIL);
        }
        return ro;
    }

    @PatchMapping(value = "/modify")
    @Permission(value = 1,tags = Constants.API_SUPER)
    public Ro<Role> modify(@RequestBody OperateMo operateMo){
        Ro<Role> ro = new Ro<>();
        final ServiceRo serviceRo = operateMo.updateBuild(Role.class, null);
        if (!serviceRo.getResult()){
            return ro.setResultEnum(RoState.FAIL).setMessage(serviceRo.getMessage());
        }
        if (operateMo.getEntity().size()==0){
            return ro.setResultEnum(RoState.FAIL).setMessage("无操作参数传入,请传参后重试");
        }
        if (Objects.isNull(operateMo.get("id"))){
            return ro.setResultEnum(RoState.FAIL).setMessage("缺少[id]字段,请传参后重试");
        }
        if (!roleService.update(operateMo)){
            ro.setResultEnum(RoState.FAIL).setMessage("参数无匹配,修改失败");
        }
        return ro;
    }

    @DeleteMapping(value = "/delete/{id}")
    @Permission(value = 1,tags = Constants.API_SUPER)
    public Ro<Role> delete(@PathVariable Integer id){
        Ro<Role> ro = new Ro<>();
        QueryMo queryMo = new QueryMo();
        queryMo.put("id",id);
        queryMo.build(Role.class,null);
        if (!roleService.delete(queryMo)){
            ro.setResultEnum(RoState.FAIL).setMessage("无匹配ID,删除失败");
        }
        return ro;
    }

    @DeleteMapping(value = "/delete/logical/{id}")
    @Permission(value = 1,tags = Constants.API_SUPER)
    public Ro<Role> logicallyDelete(@PathVariable Integer id){
        Ro<Role> ro = new Ro<>();
        QueryMo queryMo = new QueryMo();
        queryMo.put("id",id);
        queryMo.build(Role.class,null);
        if (!roleService.logicallyDelete(queryMo)){
            ro.setResultEnum(RoState.FAIL).setMessage("无匹配ID,删除失败");
        }
        return ro;
    }
}
